package cn.gcyl.module.biz.dal.mysql.thirdpartyinformation;

import java.util.*;

import cn.gcyl.framework.common.pojo.PageResult;
import cn.gcyl.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.gcyl.framework.mybatis.core.mapper.BaseMapperX;
import cn.gcyl.module.biz.dal.dataobject.specialmeeting.SpecialMeetingDO;
import cn.gcyl.module.biz.dal.dataobject.thirdpartyinformation.ThirdPartyInformationDO;
import org.apache.ibatis.annotations.Mapper;
import cn.gcyl.module.biz.controller.admin.thirdpartyinformation.vo.*;

import static cn.gcyl.framework.web.core.util.WebFrameworkUtils.getLoginUserId;

/**
 * 第三方信息管理 Mapper
 *
 * @author 芋道源码
 */
@Mapper
public interface ThirdPartyInformationMapper extends BaseMapperX<ThirdPartyInformationDO> {

    default PageResult<ThirdPartyInformationDO> selectPage(ThirdPartyInformationPageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<ThirdPartyInformationDO>()
                .eqIfPresent(ThirdPartyInformationDO::getThirdPartyId, reqVO.getThirdPartyId())
                .likeIfPresent(ThirdPartyInformationDO::getTitle, reqVO.getTitle())
                .eqIfPresent(ThirdPartyInformationDO::getId, reqVO.getId())
                //必传 通过类型分为三个页面
                .eqIfPresent(ThirdPartyInformationDO::getInfoType, reqVO.getInfoType())
                .eqIfPresent(ThirdPartyInformationDO::getCategory, reqVO.getCategory())
                .eqIfPresent(ThirdPartyInformationDO::getArea, reqVO.getArea())
                .betweenIfPresent(ThirdPartyInformationDO::getPublishDate, reqVO.getPublishDate())
                .orderByDesc(ThirdPartyInformationDO::getPublishDate));
    }

    default PageResult<ThirdPartyInformationDO> getAppThirdPartyInformationPage(AppThirdPartyInformationPageReqVO reqVO) {
        LambdaQueryWrapperX<ThirdPartyInformationDO> queryWrapper = new LambdaQueryWrapperX<>();
                queryWrapper.eqIfPresent(ThirdPartyInformationDO::getThirdPartyId, reqVO.getThirdPartyId());
                queryWrapper.likeIfPresent(ThirdPartyInformationDO::getTitle, reqVO.getTitle());
                queryWrapper.likeIfPresent(ThirdPartyInformationDO::getArea, reqVO.getArea());
                queryWrapper.likeIfPresent(ThirdPartyInformationDO::getInfoType, reqVO.getColumns());
                // 支持标题的模糊匹配（LIKE）
                if (reqVO.getSubscriptionWord() != null && reqVO.getSubscriptionWord().length > 0) {
                    queryWrapper.and(wrapper -> {
                        for (String name : reqVO.getSubscriptionWord()) {
                            wrapper.or().like(ThirdPartyInformationDO::getTitle, name);
                        }
                    });
                }
                //必传 通过类型分为三个页面
                queryWrapper.eqIfPresent(ThirdPartyInformationDO::getInfoType, reqVO.getInfoType());
                queryWrapper.orderByDesc(ThirdPartyInformationDO::getCreateTime);
        return selectPage(reqVO, queryWrapper);
    }
    default PageResult<ThirdPartyInformationDO> getAppThirdPartyInformationList(AppThirdPartyInformationListReqVO reqVO) {
        Long loginUserId = getLoginUserId();
        LambdaQueryWrapperX<ThirdPartyInformationDO> queryWrapper = new LambdaQueryWrapperX<>();

        // 基础条件
        queryWrapper.likeIfPresent(ThirdPartyInformationDO::getArea, reqVO.getArea());

        // 支持标题的模糊匹配（LIKE）
        if (reqVO.getTitle() != null && reqVO.getTitle().length > 0) {
            queryWrapper.and(wrapper -> {
                for (String title : reqVO.getTitle()) {
                    wrapper.or().like(ThirdPartyInformationDO::getTitle, title);
                }
            });
        }

        // 添加根据用户ID查询咨询用户IDs的条件
        if (loginUserId != null) {
            queryWrapper.apply("FIND_IN_SET({0}, user_collect_ids)", loginUserId);
        }

        // 排序
        queryWrapper.orderByDesc(ThirdPartyInformationDO::getPublishDate);

        // 使用分页查询，传入reqVO参数
        return selectPage(reqVO, queryWrapper);
    }
}